package com.cans.lightning.business.lowcode.web;

import com.cans.lightning.base.dto.ResDto;
import com.cans.lightning.business.lowcode.dto.AppTableFieldDto;
import com.cans.lightning.business.lowcode.manager.form.bean.AppFormBean;
import com.cans.lightning.business.lowcode.manager.form.cache.AppFormBeanCacheManager;
import com.cans.lightning.business.lowcode.service.api.IAppFormDefinitionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * 表单
 *
 * @author shencan
 * @date 2020/6/14 22:14
 */
@RestController
@RequestMapping("/app_form_design")
public class AppFormDesignController  {

    @Resource
    private IAppFormDefinitionService appFormService;
    @Resource
    private AppFormBeanCacheManager appFormBeanCacheManager;

    @GetMapping("/designBean/{id}")
    public ResDto<AppFormBean> designBean(@PathVariable String id) {
        return ResDto.success(appFormService.formBeanDesign(id));
    }

    @PostMapping("/mergeBaseInfo")
    public ResDto<String> mergeBaseInfo(@RequestBody AppFormBean appFormBean) throws ExecutionException {
        appFormService.mergeBaseInfo(appFormBean);
        return ResDto.success();
    }

    @GetMapping("/removeEdit/{formId}")
    public ResDto<String> removeEdit( @PathVariable String formId){
        appFormService.removeEdit(formId);
        return ResDto.success();
    }
    @PostMapping("/saveToDb")
    public ResDto<String> saveToDb(@RequestBody AppFormBean appFormBean) throws ExecutionException {
        appFormService.saveToDb(appFormBean);
        appFormBeanCacheManager.invalidate(appFormBean.getId());
        return ResDto.success();
    }

    /**
     * 获取下一个字段索引ID
     * @param formId
     * @return
     */
    @GetMapping("/getNextFieldIndex/{formId}")
    public ResDto<String> getNextFieldIndex(@PathVariable String formId) {
        return ResDto.success(appFormService.getNextFieldIndex(formId));
    }

    @PostMapping("/addField")
    public ResDto<AppTableFieldDto> addField(@RequestBody AppTableFieldDto tableFieldDto) throws ExecutionException {
        return ResDto.success(appFormService.addField(tableFieldDto));
    }
}
